/*
 * Copyright (c) 2021, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.filter.convolve.border;

import boofcv.generate.AutoTypeImage;
import boofcv.generate.CodeGeneratorBase;

import java.io.FileNotFoundException;

/**
 * Code generator for {@link ConvolveJustBorder_General_IL}.
 *
 * @author Peter Abeles
 */
public class GenerateConvolveJustBorder_General_IL extends CodeGeneratorBase {

	AutoTypeImage imageOut;
	String typeKernel;
	String typeInput;
	String dataKernel;
	String sumType;

	@Override
	public void generateCode() throws FileNotFoundException {

		printPreamble();

		createMethod(AutoTypeImage.F32);
		createMethod(AutoTypeImage.I16);
		createMethod(AutoTypeImage.S32);

		out.println("}");
	}

	public void createMethod( AutoTypeImage imageOut ) {
		this.imageOut = imageOut;

		typeKernel = imageOut.getKernelType();
		dataKernel = imageOut.getKernelDataType();
		typeInput = "ImageBorder_IL_"+typeKernel;

		sumType = imageOut.getSumType();
		String typeCast = imageOut.getTypeCastFromSum();

		addHorizontal(typeCast);
		addVertical(typeCast);
		addConvolution(typeCast);
	}

	public void printPreamble() {
		out.print("import boofcv.core.image.border.ImageBorder_IL_F32;\n" +
				"import boofcv.core.image.border.ImageBorder_IL_S32;\n" +
				"import boofcv.struct.convolve.*;\n" +
				"import boofcv.struct.image.*;\n" +
				"import java.util.Arrays;\n" +
				"\n" +
				"/**\n" +
				" * <p>\n" +
				" * Convolves just the image's border. How the border condition is handled is specified by the {@link boofcv.core.image.border.ImageBorder}\n" +
				" * passed in. For 1D kernels only the horizontal or vertical borders are processed.\n" +
				" * </p>\n" +
				" * \n" +
				" * <p>\n" +
				" * WARNING: Do not modify. Automatically generated by "+getClass().getSimpleName()+".\n" +
				" * </p>\n" +
				" * \n" +
				" * @author Peter Abeles\n" +
				" */\n" +
				"public class "+className+" {\n\n");
	}

	public void addHorizontal( String typeCast ) {
		String typeOutput = imageOut.getInterleavedName();

		out.print("\tpublic static void horizontal(Kernel1D_"+typeKernel+" kernel, "+typeInput+" src, "+typeOutput+" dst ) {\n" +
				"\t\tfinal int offset = kernel.getOffset();\n" +
				"\t\tfinal int kernelWidth = kernel.getWidth();\n" +
				"\t\tfinal int width = dst.getWidth();\n" +
				"\t\tfinal int height = dst.getHeight();\n" +
				"\t\tfinal int borderRight = kernelWidth-offset-1;\n" +
				"\t\tfinal int numBands = dst.getNumBands();\n" +
				"\n" +
				"\t\t"+sumType+" pixel[] = new "+sumType+"[ numBands ];\n" +
				"\t\t"+sumType+" total[] = new "+sumType+"[ numBands ];\n" +
				"\n" +
				"\t\tfor (int y = 0; y < height; y++) {\n" +
				"\t\t\tint indexDst = dst.startIndex + y * dst.stride;\n" +
				"\n" +
				"\t\t\tfor ( int x = 0; x < offset; x++ ) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tfor (int k = 0; k < kernelWidth; k++) {\n" +
				"\t\t\t\t\tsrc.get(x+k-offset,y, pixel);\n" +
				"\t\t\t\t\t"+sumType+" valueK = kernel.data[k];\n" +
				"\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\ttotal[band] += pixel[band]*valueK;\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst++] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\n" +
				"\t\t\tindexDst = dst.startIndex + y * dst.stride + (width-borderRight)*numBands;\n" +
				"\t\t\tfor ( int x = width-borderRight; x < width; x++ ) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tfor (int k = 0; k < kernelWidth; k++) {\n" +
				"\t\t\t\t\tsrc.get(x+k-offset,y, pixel);\n" +
				"\t\t\t\t\t"+sumType+" valueK = kernel.data[k];\n" +
				"\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\ttotal[band] += pixel[band]*valueK;\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst++] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void addVertical( String typeCast ) {
		String typeOutput = imageOut.getInterleavedName();

		out.print("\tpublic static void vertical(Kernel1D_"+typeKernel+" kernel, "+typeInput+" src, "+typeOutput+" dst ) {\n" +
				"\t\tfinal int offset = kernel.getOffset();\n" +
				"\t\tfinal int kernelWidth = kernel.getWidth();\n" +
				"\t\tfinal int width = dst.getWidth();\n" +
				"\t\tfinal int height = dst.getHeight();\n" +
				"\t\tfinal int borderBottom = kernelWidth-offset-1;\n" +
				"\t\tfinal int numBands = dst.getNumBands();\n" +
				"\n" +
				"\t\t"+sumType+" pixel[] = new "+sumType+"[ numBands ];\n" +
				"\t\t"+sumType+" total[] = new "+sumType+"[ numBands ];\n" +
				"\n" +
				"\t\tfor ( int x = 0; x < width; x++ ) {\n" +
				"\t\t\tint indexDst = dst.startIndex + x*numBands;\n" +
				"\n" +
				"\t\t\tfor (int y = 0; y < offset; y++, indexDst += dst.stride) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tfor (int k = 0; k < kernelWidth; k++) {\n" +
				"\t\t\t\t\tsrc.get(x,y+k-offset,pixel);\n" +
				"\t\t\t\t\t"+sumType+" valueK = kernel.data[k];\n" +
				"\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\ttotal[band] += pixel[band] * valueK;\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst + band] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\n" +
				"\t\t\tindexDst = dst.startIndex + (height-borderBottom) * dst.stride + x*numBands;\n" +
				"\t\t\tfor (int y = height-borderBottom; y < height; y++, indexDst += dst.stride) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tfor (int k = 0; k < kernelWidth; k++ ) {\n" +
				"\t\t\t\t\tsrc.get(x,y+k-offset, pixel);\n" +
				"\t\t\t\t\t"+sumType+" valueK = kernel.data[k];\n" +
				"\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\ttotal[band] += pixel[band] * valueK;\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst + band] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void addConvolution( String typeCast ) {
		String typeOutput = imageOut.getInterleavedName();

		out.print("\tpublic static void convolve(Kernel2D_"+typeKernel+" kernel, "+typeInput+" src, "+typeOutput+" dst ) {\n" +
				"\t\tfinal int offsetL = kernel.getOffset();\n" +
				"\t\tfinal int offsetR = kernel.getWidth()-offsetL-1;\n" +
				"\t\tfinal int width = dst.getWidth();\n" +
				"\t\tfinal int height = dst.getHeight();\n" +
				"\t\tfinal int numBands = dst.getNumBands();\n" +
				"\n" +
				"\t\t"+sumType+" pixel[] = new "+sumType+"[ numBands ];\n" +
				"\t\t"+sumType+" total[] = new "+sumType+"[ numBands ];\n" +
				"\n" +
				"\t\t// convolve along the left and right borders\n" +
				"\t\tfor (int y = 0; y < height; y++) {\n" +
				"\t\t\tint indexDst = dst.startIndex + y * dst.stride;\n" +
				"\n" +
				"\t\t\tfor ( int x = 0; x < offsetL; x++ ) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tint indexKer = 0;\n" +
				"\t\t\t\tfor( int i = -offsetL; i <= offsetR; i++ ) {\n" +
				"\t\t\t\t\tfor (int j = -offsetL; j <= offsetR; j++) {\n" +
				"\t\t\t\t\t\tsrc.get(x+j,y+i, pixel);\n" +
				"\t\t\t\t\t\t"+sumType+" valueK = kernel.data[indexKer++];\n" +
				"\t\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\t\ttotal[band] += pixel[band] * valueK;\n" +
				"\t\t\t\t\t\t}\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst++] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\n" +
				"\t\t\tindexDst = dst.startIndex + y * dst.stride + (width-offsetR)*numBands;\n" +
				"\t\t\tfor ( int x = width-offsetR; x < width; x++ ) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tint indexKer = 0;\n" +
				"\t\t\t\tfor( int i = -offsetL; i <= offsetR; i++ ) {\n" +
				"\t\t\t\t\tfor (int j = -offsetL; j <= offsetR; j++) {\n" +
				"\t\t\t\t\t\tsrc.get(x+j,y+i, pixel);\n" +
				"\t\t\t\t\t\t"+sumType+" valueK = kernel.data[indexKer++];\n" +
				"\t\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\t\ttotal[band] += pixel[band] * valueK;\n" +
				"\t\t\t\t\t\t}\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst++] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\n" +
				"\t\t// convolve along the top and bottom borders\n" +
				"\t\tfor ( int x = offsetL; x < width-offsetR; x++ ) {\n" +
				"\t\t\tint indexDst = dst.startIndex + x*numBands;\n" +
				"\n" +
				"\t\t\tfor (int y = 0; y < offsetL; y++, indexDst += dst.stride) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tint indexKer = 0;\n" +
				"\t\t\t\tfor( int i = -offsetL; i <= offsetR; i++ ) {\n" +
				"\t\t\t\t\tfor (int j = -offsetL; j <= offsetR; j++) {\n" +
				"\t\t\t\t\t\tsrc.get(x+j,y+i, pixel);\n" +
				"\t\t\t\t\t\t"+sumType+" valueK = kernel.data[indexKer++];\n" +
				"\t\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\t\ttotal[band] += pixel[band] * valueK;\n" +
				"\t\t\t\t\t\t}\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst + band] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\n" +
				"\t\t\tindexDst = dst.startIndex + (height-offsetR) * dst.stride + x*numBands;\n" +
				"\t\t\tfor (int y = height-offsetR; y < height; y++, indexDst += dst.stride) {\n" +
				"\t\t\t\tArrays.fill(total,0);\n" +
				"\t\t\t\tint indexKer = 0;\n" +
				"\t\t\t\tfor( int i = -offsetL; i <= offsetR; i++ ) {\n" +
				"\t\t\t\t\tfor (int j = -offsetL; j <= offsetR; j++) {\n" +
				"\t\t\t\t\t\tsrc.get(x+j,y+i, pixel);\n" +
				"\t\t\t\t\t\t"+sumType+" valueK = kernel.data[indexKer++];\n" +
				"\t\t\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\t\t\ttotal[band] += pixel[band] * valueK;\n" +
				"\t\t\t\t\t\t}\n" +
				"\t\t\t\t\t}\n" +
				"\t\t\t\t}\n" +
				"\t\t\t\tfor (int band = 0; band < numBands; band++) {\n" +
				"\t\t\t\t\tdst.data[indexDst + band] = "+typeCast+"total[band];\n" +
				"\t\t\t\t}\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public static void main( String[] args ) throws FileNotFoundException {
		GenerateConvolveJustBorder_General_IL generator = new GenerateConvolveJustBorder_General_IL();
		generator.generateCode();
	}
}
